using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.IO;
using System.Runtime.Remoting;
using Dimok;
using ConfigPDF = Dimok.Config.imaging.PDF;
using Dimok.Data;
using Dimok.Images;
//using OCR = Dimok.Documents.Ocr;

namespace AcrobatObjects {
	public class AcrobatClient {
		public static string TCP{get{return ConfigPDF.TCP;}}

		private static RemotePdf ActivateRemotePdf(){	return RemotePdf.ActivateRemotePdf(TCP); }

		public static byte[] CombineWord(string[] fileNames,DataTable formTable,bool pdfOutput){
			try{
				return ActivateRemotePdf().CombineWordToPDF(FileManager.GetBytesFromFile(fileNames),formTable,pdfOutput);
			}catch(Exception exc){throw new Exception(TCP,exc);}
		}
		//public static byte[] CombineWordToPDF(string[] fileNames,string connection,string sql){
		//	return ActivateRemotePdf().CombineWordToPDF(FileManager.GetBytesFromFile(fileNames),connection,sql);
		//}

		/*
		public static byte[] CombineToPDF(string[] fileNames){
			ArrayList alFiles = new ArrayList();
			foreach( string fileName in fileNames )
				alFiles.Add(FileManager.GetBytesFromFile(fileName));
			return ActivateRemotePdf().CombineToPDF((byte[][])alFiles.ToArray(typeof(byte[])));
		}
*/
		public static byte[] FillForm(byte[] file,DataTable fields){
			switch(Enums.GetPageType(file)){
				case PageTypes.PDF: return FillPDFForm(file,fields.Rows[0]);
				case PageTypes.WORD: return FillWordForm(file,fields);
			}
			return file;
			//throw new Exception(TCP+Environment.NewLine+"Unsupported file type["+Enums.GetContentType(file)+"]");
		}
		public static byte[] FillPDFForm(byte[] fileIn,DataRow dataRow) {
			//StringDictionary fields = new StringDictionary();
			Hashtable fields = new Hashtable();
			try{
				DataColumnCollection cols = dataRow.Table.Columns;
				foreach( DataColumn col in cols )
					fields.Add(col.ColumnName,dataRow[col.ColumnName].ToString());
				return FillPDFForm(fileIn,fields);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] FillPDFForm(byte[] fileIn,Hashtable fields) {
			try{
				return ActivateRemotePdf().FillPDFForm(fileIn,fields);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] FillPDFForm(byte[][] filesIn,Hashtable fields){
			try{
				return ActivateRemotePdf().FillPDFForm(filesIn,fields);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] FillWordForm(string fileName,DataTable fields){
			try{
				return FillWordForm(FileManager.GetBytesFromFile(fileName),fields);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] FillWordForm(byte[] fileIn,DataTable fields){
			try{
				return ActivateRemotePdf().FillWordForm(fileIn,fields);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] FillWordForm(byte[][] filesIn,DataTable fields){
			try{
				return ActivateRemotePdf().FillWordForm(filesIn,fields);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] TiffToPDF(byte[] fileIn){
			try{
				return ActivateRemotePdf().TiffToPDF(fileIn);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] CombinePDF(byte[][] pdfPages){
			try{
				if( pdfPages.Length == 1 )return pdfPages[0];
				return ActivateRemotePdf().CombinePDF(pdfPages);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static byte[] GetPages(byte[] file,string pages){
			try{
				if( FileManager.IsAllPages(pages) )return file;
				return ActivateRemotePdf().GetPages(file,pages);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static string[] GetText(byte[] file,params int[] pages){
			try{
				return ActivateRemotePdf().GetText(file,pages);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static string AnyToPDF(string fileName,string fileOut,bool createNew){
			try{
				return FileManager.GetFileFromBytes(ActivateRemotePdf().AnyToPDF(FileManager.GetBytesFromFile(fileName)),fileOut,createNew);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static string Decript(string fileName,params int[] pageNumbers){
			try{
				return FileManager.GetFileFromBytes(ActivateRemotePdf().Decrypt(FileManager.GetBytesFromFile(fileName),pageNumbers));
			}catch(Exception exc){ throw new Exception("FileName:"+fileName+Environment.NewLine+TCP+Environment.NewLine,exc); }
		}
		/// <summary>
		/// Prepare SubmitButton
		/// </summary>
		/// <param name="buttonTemplate"></param>
		/// <param name="buttonField"></param>
		/// <param name="actionWebPage"></param>
		/// <param name="flags">4 - HTML,256 - whole document</param>
		/// <returns></returns>
		public static byte[] PrepButtonTemplate(byte[] buttonTemplate,string buttonField,string actionWebPage,int flags) {
			try{
				if( ConfigPDF.Remote ){
					return ActivateRemotePdf().PrepButtonTemplate(buttonTemplate,buttonField,actionWebPage,flags);
				}else return Dimok.Documents.PDF.PrepButtonTemplate(buttonTemplate,buttonField,actionWebPage,flags);
			}catch(Exception exc){ throw new Exception(TCP+Environment.NewLine,exc); }
		}
		public static Hashtable AnalizeForm(byte[] pdfFile,DataTable tblFormMap) {
			return ActivateRemotePdf().AnalizeForm(pdfFile,tblFormMap);
		}
		public static DataTable ReadForm(byte[] file, DataSet dsMatrix) {
			return ActivateRemotePdf().ReadForm(file, dsMatrix);
		}
	}
}